package com.android.thought.badboys;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.android.thought.badboys.Action.IBackupAccessAction;
import com.android.thought.badboys.Action.IPhoneAccessAction;
import com.android.thought.badboys.Action.ISmsAction;
import com.android.thought.badboys.Alarm.AlarmServices;
import com.android.thought.badboys.Common.PhoneGroupLoadTask;
import com.android.thought.badboys.Common.PhoneGroupSaveTask;
import com.android.thought.badboys.Info.DataConversion;
import com.android.thought.badboys.Info.ListBackup;
import com.android.thought.badboys.Info.PhoneInfo;
import com.android.thought.badboys.Info.SelectionPhoneInfo;

public class Model {
	
	static public AlarmServices mAlarmServives ; 
	
	public static AlarmServices getAlarmServicesInstance(Context context) {
		if ( mAlarmServives == null ) 
			mAlarmServives = new AlarmServices(context);
		
		return mAlarmServives;
	}
	
	/**
	 * 관리 대상 전화번호목록
	 */
	SelectionPhoneInfo mAdministrationPhoneInfo;
	
	/**
	 * 백업 데이터
	 */
	ListBackup mBackupData = null;
	
	Context mAppBaseContext;
	
	boolean misFileLoading = false;
	boolean mIsBackUpLoading = false;
	
	IPhoneAccessAction mWaitAccessListener = null;
	IBackupAccessAction mWaitBackupAccessListener = null;
	
	
	public void onDeleteMessageDelete(Context context, ISmsAction acion) {
		final ArrayList<PhoneInfo> list =  mAdministrationPhoneInfo.getList();
		
		Log.e("CH_DEBUG", "### 기본 크기 :  " + list.size());
		// 데이터 삭제 해야할 정보들.
		ArrayList<DataConversion> dataConversions = new ArrayList<DataConversion>();
		
		
		if ( list != null && list.size() > 0  ) {
			
			for ( PhoneInfo phoneInfo : list ) { 
				if ( mBackupData != null ) { 
					 ArrayList<DataConversion> data = mBackupData.getData();
					 boolean isNew = true;
					 for  ( DataConversion item : data) {
						 if ( item.isCompatePhoneInf(phoneInfo) ) {
							 isNew  = false;
							 dataConversions.add(item);
							 break;
						 }
					 }
					 
					 if( isNew ) {
						 // 새로운 데이터 로딩.
						 DataConversion conversion = new DataConversion();
						 conversion.setPhoneInfo(phoneInfo);
						 data.add(conversion);
						 dataConversions.add(data.get(data.size()-1));
					 }
				}
			}
		}
		
		Log.e("CH_DEBUG", "### 원복  :  " + dataConversions.size());
		
//		
		if ( dataConversions.size() > 0 )
			new MessageDeleteTask(context, 1, dataConversions ).execute(acion);
		else
			acion.onLoadComplated(false);
	}
	
	IBackupAccessAction mBackUpAccessAction = new IBackupAccessAction() {
		
		@Override
		public void onSaveComplated(boolean isSucess) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLoadComplated(ListBackup backup) {
			mIsBackUpLoading = false;
			
			if ( backup == null )
				backup = new ListBackup();
			
			mBackupData = backup;
			
			if ( mWaitBackupAccessListener != null ) 
				mWaitBackupAccessListener.onLoadComplated(mBackupData);
			
			mWaitBackupAccessListener = null;
			
		}
	};
	
	IPhoneAccessAction mPhoneAccessAction = new IPhoneAccessAction() {
		
		@Override
		public void onSaveComplated(boolean isSucess) {
		}
		
		@Override
		public void onLoadComplated(SelectionPhoneInfo info) {
			misFileLoading = false;
			
			if ( info == null )
				info = new SelectionPhoneInfo();

			mAdministrationPhoneInfo = info;
			
			if ( mWaitAccessListener != null ) 
				mWaitAccessListener.onLoadComplated(mAdministrationPhoneInfo);
			
			mWaitAccessListener = null;
		}
	};
	
	
	
	public Model(Context context) {
		this.mAppBaseContext = context;
		
		this.onLoadAdministrationPhoneInfo(context, mPhoneAccessAction);
		
		this.onLoadBackUpData(context, mBackUpAccessAction, false);
		
	}
	
	
	
	/**
	 * 외부에서 데이더 읽어 갈때 사용.
	 * @param action
	 */
	public void getBackUpInfo(Context context, IBackupAccessAction action, boolean isPro) {
		if ( mIsBackUpLoading ) { 
			// 파일 로딩 중이니..대기.
			mWaitBackupAccessListener = action;
		} else { 
			if ( mBackupData != null )
				action.onLoadComplated(mBackupData);
			else onLoadBackUpData(context, action, isPro);
		}
	}
	
	public synchronized  void setBackUpData(ListBackup info) { 
		this.mBackupData = info;
	}
	
	public synchronized void onLoadBackUpData(Context context ,IBackupAccessAction action, boolean isPro) {
		mIsBackUpLoading = true;
		new BackupFileLoadTask(context, isPro ,action).execute();
	}
	
	public synchronized void onSaveBackUpData(Context context, IBackupAccessAction action) {
		Log.e("CH_DEBUG", "#### 크기 ㅣ" + mBackupData.getData().size());
		new BackupFileSaveTask(context, action).execute(mBackupData);
	}
	
	
	/**
	 * 외부에서 데이더 읽어 갈때 사용.
	 * @param action
	 */
	public void getAdministrationPhoneInfo(Context context, IPhoneAccessAction action) {
		if ( misFileLoading ) { 
			// 파일 로딩 중이니..대기.
			mWaitAccessListener = action;
		} else { 
			if ( mAdministrationPhoneInfo != null )
				action.onLoadComplated(mAdministrationPhoneInfo);
			else onLoadAdministrationPhoneInfo(context, action);
		}
	}
	
	public synchronized  void setAdministrationPhoneInfo(SelectionPhoneInfo info) { 
		this.mAdministrationPhoneInfo  = info;
	}
	
	public SelectionPhoneInfo getAdministrationPhoneInfo() { 
		return this.mAdministrationPhoneInfo;
	}
	
	
	public synchronized void onLoadAdministrationPhoneInfo(Context context ,IPhoneAccessAction action) {
		misFileLoading = true;
		new PhoneGroupLoadTask(context, false, action).execute();
	}
	
	public synchronized void onSaveAdministrationPhoneInfo(Context context, IPhoneAccessAction action) {
		new PhoneGroupSaveTask(context, action).execute(mAdministrationPhoneInfo);
	}
	
}
